package jsu.weizixuan.likesystem.controller;

import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jsu.weizixuan.likesystem.common.BaseResponse;
import jsu.weizixuan.likesystem.common.ResultUtils;
import jsu.weizixuan.likesystem.constant.UserConstant;
import jsu.weizixuan.likesystem.model.pojo.User;
import jsu.weizixuan.likesystem.service.UserService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("user")
public class UserController {  
    @Resource
    private UserService userService;

    /**
     * 用户登录
     * @param userId 用户id
     * @param request 请求
     * @return
     */
    @GetMapping("/login")
    public BaseResponse<User> login(long userId, HttpServletRequest request) {
        User user = userService.getById(userId);  
        request.getSession().setAttribute(UserConstant.LOGIN_USER, user);
        return ResultUtils.success(user);
    }

    /**
     * 获取当前登录用户
     * @param request 请求
     * @return
     */
    @GetMapping("/get/login")
    public BaseResponse<User> getLoginUser(HttpServletRequest request) {
        User loginUser = (User) request.getSession().getAttribute(UserConstant.LOGIN_USER);
        return ResultUtils.success(loginUser);
    }

}
